<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\model\NodeModel;
use think\Controller;

class Node extends Base
{
    /**
     * 节点管理（目录形式）
    */
    public function index()
    {
        $node = new NodeModel();
        $node_list = $node->getNodeList(['is_menu'=>'1']);
        foreach($node_list as $key=>$vo){
            $node_list[$key]['style'] = "<i class='".$vo['style']."' aria-hidden='true'></i>";
            $node_list[$key]['is_menu'] = ($vo['is_menu'] == '1')?"是":"否";
        }
        $node_list_tree = rule_to_tree($node_list);
        $json_node = json_encode($node_list_tree);
        $this->assign([
            'json_tree' => $json_node
        ]);
//        dd($node_list_tree);
        return $this->fetch();
    }

    /**
     * 新增菜单功能
     * @return mixed
     */
    public function  addNode()
    {
        if(request()->isPost()){
            $param = input("post.");
            $param['action_name'] = strtolower($param['action_name']);
            $node = new NodeModel();
            $flag = $node->insertNode($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $pid = input("param.pid");
        $this->assign([
            'pid' => $pid
        ]);
        return $this->fetch();
    }

    /**
     * 删除菜单
    */
    public function nodeDel()
    {
        $id = input("param.id");
        $node = new NodeModel();
        $child_node = $node->getChild($id);
        $child_node_array  = selectArray($child_node);
        if(empty($child_node_array)){
          $flag  = $node->delNode($id);
        }else{
            $flag = ['code'=>'-1','data'=>'','msg'=>"抱歉请先删除子节点"];
        }
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }

    /**
     * 编辑菜单
    */
    public function editNode()
    {
        $node = new NodeModel();
        if(request()->isPost()){
            $param = input("post.");
            $param['action_name'] = strtolower($param['action_name']);
            $flag = $node->editNode($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input("param.id");
        $this->assign([
            'node_list' => $node->getNodeFind($id)
        ]);
        return $this->fetch();
    }


    /**
     * 节点管理（列表形式）
     */
    public function nodelist()
    {
        $node = new NodeModel();
        $node_list = $node->getNodeList(['is_menu'=>'1']);
        foreach($node_list as $key=>$vo){
            $node_list[$key]['style'] = "<i class='".$vo['style']."' aria-hidden='true'></i>";
            $node_list[$key]['is_menu'] = ($vo['is_menu'] == '1')?"是":"否";
        }

        $node_list_tree = rule_to_list($node_list);

        $this->assign('list',$node_list_tree);
        $this->assign('i',1);
        return $this->fetch();

    }


}